package com.tinyideatech.admin.controller.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.system.entity.CarCode;
import com.tinyideatech.system.service.ICarCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhangy-5
 * @date 2019-09-09
 */
@Api("车机端激活码")
@Controller
@RequestMapping("/car/code")
public class CarCodeController extends BaseController {

    @Autowired
    private ICarCodeService carCodeService;

    @GetMapping()
    public String list() {
        return "car/code/list";
    }


    @GetMapping("/add")
    public String add() {
        return "car/code/add";
    }

    @GetMapping("/edit/{id}")
    public String carCodeEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        CarCode carCode = carCodeService.getById(id);
        map.addAttribute("carCode", carCode);
        return "car/code/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(CarCode carCode) {
        QueryWrapper<CarCode> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carCode.getCode())) {
            qw.like("code", carCode.getCode());
        }
        if (StringUtils.isNotEmpty(carCode.getDeviceId())) {
            qw.like("device_id", carCode.getDeviceId());
        }
        IPage<CarCode> page = startPage();
        IPage<CarCode> carCodeList = carCodeService.page(page, qw);
        return page(carCodeList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            CarCode carCode
    ) {
        carCodeService.saveOrUpdate(carCode);
        return success();
    }

    @ResponseBody
    @PostMapping("/verify")
    @ApiOperation(value = "车机ADAS激活码验证")
    public ApiResult verify(
            @RequestParam String code
    ) {
        if (carCodeService.list(new QueryWrapper<CarCode>().eq("code", code)).size() != 0) {
            return error("403");
        }
        return success();
    }

    @ResponseBody
    @PostMapping("/nabto/verify")
    @ApiOperation(value = "车机nabto激活码验证")
    public ApiResult nabtoverify(
            @RequestParam String code
    ) {
        List<CarCode> carCodeList = carCodeService.list(new QueryWrapper<CarCode>()
                .eq("code", code));
        if (carCodeList == null) {
            return error("nabto code is not exist");
        }
        if (carCodeList.size() != 0) {
            CarCode carCode = carCodeList.get(0);
            if (carCode.getStatus() == 1) {
                return error("code has been actived");
            } else {
                return success("code is available");
            }
        }
        return error("nabto code is not exist");
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] carCodeIds = Convert.toLongArray(ids);
        carCodeService.removeByIds(Arrays.asList(carCodeIds));
        return success();
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public ApiResult changeStatus(CarCode carCode) {
        carCodeService.changeStatus(carCode);
        return success();
    }

}
